/**
 * Preorder.java
 * @author: liuex
 * 2008-3-22 ����11:45:06
 *
 * note: 
 */
package org.lex.utils.tree;

import java.util.Iterator;

import org.lex.utils.collections.ArrayStack;
import org.lex.utils.collections.Stack;


/**
 * ǰ�������
 */
public class Preorder implements Iterator<Node> {
	private Stack<Node> stack = null;

	public Preorder(Node root) {
		this.stack = new ArrayStack<Node>();
		this.stack.push(root);
	}

	@Override
	public boolean hasNext() {
		return !stack.isEmpty();
	}

	@Override
	public Node next() {
		Node top = stack.pop();
		int childCount = top.getChildCount();
		if (0 != childCount) {
			for (int i = childCount - 1; i >= 0; i--)
				stack.push(top.getChildAt(i));
		}
		return top;
	}

	@Override
	public void remove() throws UnsupportedOperationException {
		throw new UnsupportedOperationException();
	}
}
